package com.platform.framework.controller.rights;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.JsonUtils;
import com.platform.common.utils.PageUtils;
import com.platform.framework.controller.BaseController;
import com.platform.framework.entity.rights.FwPlModule;
import com.platform.framework.service.rights.FwPlModuleService;

/**
 * 
 * 组织结构类
 * 
 * @author 王求卿[wangqiuqing] Tel:18101037995
 * @createDate 2017-7-7 下午5:16:55
 */
@RestController
@RequestMapping("/module")
public class FwPlModuleController extends BaseController {

	@Autowired
	FwPlModuleService fwPlModuleService;
	
	
	
	
	/**
	 * 
	  * 来源系统列表 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	  * @throws Exception
	  * @createDate 2017-7-21 下午2:30:35
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "serverlist", method = { RequestMethod.GET })
	public String serverList(HttpServletRequest request) throws Exception {
			return JSONArray.toJSONString(fwPlModuleService.getServerMenu());
	}
	
	/**
	 * 
	  * 根据systemId获取组织机构的mini tree menu 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	  * @throws Exception
	  * @createDate 2018年5月29日10:27:09
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "menulist", method = { RequestMethod.POST })
	public String modulemenuList(HttpServletRequest request) throws Exception {
		String moduleId = request.getParameter("systemId");
		String menuJson =  fwPlModuleService.getModulesByUserId(moduleId,this.getUserId());
		return menuJson;
	}
	
	/**
	 * 
	  * 上级组织模块的菜单列表 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	  * @throws Exception
	  * @createDate 2017-7-21 下午2:29:52
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "treelist", method = { RequestMethod.POST })
	public String moduletreeList(HttpServletRequest request) throws Exception {
		String moduleId = request.getParameter("moduleId");
		String opt = request.getParameter("opt");//如果是添加操作则执行全部菜单，并选中当前的节点操作。
		System.out.println(opt);
		if( "add".equals(opt) || StringUtils.isEmpty(moduleId)){
			return JSONArray.toJSONString(fwPlModuleService.getModulesMenu());
		}else{
			return JSONArray.toJSONString(fwPlModuleService.getModulesMenu(moduleId));
		}
		
	}
	/**
	 * 
	 * org object数据
	 * 
	 * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	 * @param request
	 * @return 模块权限object数据
	 * @throws Exception 
	 * @createDate 2017-7-7 下午1:58:29
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "list", method = { RequestMethod.POST })
	public String moduleList(HttpServletRequest request) throws Exception {
		return JSONArray.toJSONString(fwPlModuleService.getAllModuleInfo());
	}

	/**
	 * 
	 * module map 数据 提高效率
	 * 
	 * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	 * @param request
	 * @return 模块权限 树Map数据
	 * @throws Exception
	 * @createDate 2017-7-7 下午1:58:39
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "maplist", method = { RequestMethod.GET })
	public String moduleMap(HttpServletRequest request) throws Exception {
		return JSONArray.toJSONString(fwPlModuleService.getAllModuleMap());
	}

	/**
	 * 
	 * 查询所有的数据
	 * 
	 * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	 * @param request
	 * @return module分页语句
	 * @throws Exception
	 * @createDate 2017-7-10 上午9:28:17
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "pagelist", method = { RequestMethod.POST })
	public String modulePageList(HttpServletRequest request) throws Exception {
		String searchCtx = request.getParameter("searchPhrase");// 查询条件框
		String parentId = request.getParameter("parentId");
		Pageable pageable = null;
		try {
			pageable = PageUtils.dealminiRequest(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Page<FwPlModule> pages = null;
		if (StringUtils.isEmpty(parentId)) {
			pages = fwPlModuleService.findModulePage(searchCtx, pageable);
		} else {
			pages = fwPlModuleService.findParentPage(parentId, pageable);
		}
		return JsonUtils.dealMiniPage(pages);
	}

	/**
	 * 
	 * 添加权限模块
	 * 
	 * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	 * @return
	 * @createDate 2017-7-13 上午9:36:57
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	 @PreAuthorize("hasAuthority('module:add')")
	@RequestMapping(value = "add", method = { RequestMethod.POST })
	public String moduleSave(HttpServletRequest request) {
		String data = request.getParameter("data");
		FwPlModule fwPlModule = JSONObject.parseObject(data, FwPlModule.class);
		fwPlModule.setId(this.getUUID());
		fwPlModule.setCreateTime(new Date());
		fwPlModule.setCreateUserCode(this.getUserId());// 操作用户
		fwPlModule.setCreateUserName(this.getUserCode());// 操作编码
		fwPlModule.setIsDeleted("0");// 未删除
		fwPlModule.setIsEnable("1");// 可用
		return fwPlModuleService.saveModule(fwPlModule);
	}

	/**
	 * 
	 * 更新权限模块
	 * 
	 * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	 * @return
	 * @createDate 2017-7-13 上午9:36:57
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	 @PreAuthorize("hasAuthority('module:update')")
	@RequestMapping(value = "update", method = { RequestMethod.POST })
	public String moduleUpdate(HttpServletRequest request) {
		String data = request.getParameter("data");
		FwPlModule fwPlModule = JSONObject.parseObject(data, FwPlModule.class);
		fwPlModule.setUpdateTime(new Date());
		fwPlModule.setUpdateUserCode(this.getUserId());
		fwPlModule.setUpdateUserName(this.getUserCode());
		fwPlModule.setIsDeleted("0");
		return fwPlModuleService.updateModule(fwPlModule);
	}

	/**
	 * 
	 * 删除权限模块
	 * 
	 * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	 * @return
	 * @createDate 2017-7-13 上午9:36:57
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	 @PreAuthorize("hasAuthority('module:delete')")
	@RequestMapping(value = "delete", method = { RequestMethod.DELETE })
	public String moduleDelete(HttpServletRequest request) {
		String ids = request.getParameter("ids");
		return fwPlModuleService.deleteModule(ids);
	}

	/**
	 * 
	 * 模块权限维护主页
	 * 
	 * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	 * @return
	 * @createDate 2017-7-13 上午9:37:19
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	 @PreAuthorize("hasAuthority('module')")//有用户添加权限的才能访问
	@RequestMapping("/index")
	public ModelAndView moduleIndex() {
		ModelAndView mv = new ModelAndView("rights/module/index");
		return mv;
	}

	/**
	 * 
	 * 模块权限添加，修改页面界面
	 * 
	 * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	 * @return
	 * @createDate 2017-7-13 上午9:37:43
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping("/edit")
	public ModelAndView moduleAdd(HttpServletRequest request) {
		String moduleId = request.getParameter("moduleId");
		String opt = request.getParameter("opt");
		ModelAndView mv = new ModelAndView("rights/module/edit");
		if(!StringUtils.isEmpty(moduleId)){
			mv.addObject("moduleId", moduleId);
			mv.addObject("opt",opt);
		}
		return mv;
	}

}